﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace RattlerRace
{
    /// <summary>
    /// Razred Apple, avtor Denis Jakoša
    /// </summary>
    public class Apple : Block
    {
        private bool _isGold;
        private bool _wasGold;
        private int _random;
        private int _counter;

        public Apple()
        {
            _random = 1;
            _isGold = false;
            _wasGold = false;
            this.State = 1;
            this.Counter = 100; //spremenimo sproti
        }

        public Apple(int levelNum)
        {
            _random = levelNum; //verjetnost, da bo jabolko zlato, se povečuje z leveli
            _isGold = false;
            _wasGold = false;
            this.State = 1;
            this.Counter = 100; //spremenimo sproti
        }

        public bool IsGold
        {
            get { return _isGold; }
            set { _isGold = value; }
        }

        public bool WasGold
        {
            get { return _wasGold; }
            set { _wasGold = value; }
        }

        public int Random
        {
            get { return _random; }
            set { _random = value; }
        }

        public int Counter
        {
            get { return _counter; }
            set { _counter = value; }
        }

        /// <summary>
        /// Metoda generira seznam jabolk na naključnih mestih na igralni površini.
        /// </summary>
        /// <param name="existingApples"></param>
        public static void generateApples(ref List<Apple> existingApples, ref Block[,] gameGrid)
        {
            /* KOMENTAR
             * V dokumentaciji funkcija sprejme samo seznam jabolk: generateApples(ref List<Apple> existingApples), menim pa, da rabimo še celotno mrežo,
             * ker lahko drugače jabolku damo koordinate, kjer je gor stena ali pa začetni položaj kač!
            */
            Random R = new Random();
            Apple tmpApple;
            int currentCount = existingApples.Count;
            int applesNum;
            int x, y;

            if (currentCount <= 10)
                applesNum = 10;
            else
                applesNum = 20 - currentCount;

            applesNum = 1; //TEST

            for (int i = 0; i < applesNum; i++)
            {
                while(true)
                {
                    x = R.Next(gameGrid.GetLength(0));
                    y = R.Next(gameGrid.GetLength(1));
                    if((gameGrid[x, y] == null || gameGrid[x, y] is BlankBlock) && !(gameGrid[x, y] is Apple))
                    {
                        tmpApple = new Apple();
                        tmpApple.P = new Point(x, y);
                        gameGrid[x, y] = tmpApple;
                        existingApples.Add(tmpApple);
                        break;
                    }
                }
                
            }
        }

        /// <summary>
        /// Preveri, če je spremenljivka random na pravi vrednosti.
        /// </summary>
        /// <param name="randomTest">Naključna za primerjavo in določanje spremembe v zlato jabolko.</param>
        public void tryGold(int randomTest)
        {
            /* KOMENTAR
             * V dokumentaciji je metoda brez argumenta: void tryGold(). S čim naj primerjamo _random?
             */
            if (randomTest <= _random && !_wasGold)
            {
                _isGold = true;
                _wasGold = true;
            }
        }
        
        /// <summary>
        /// Metoda preveri jabolka, če so zlata. Zlatim odšteva števec, ko je ta enak 0, pa jabolko spremeni v rdečo in postavi zastavico,
        /// da se ne more več spremeniti v zlato.
        /// </summary>
        /// <param name="existingApples">Seznam jabolk, ki so na igralni površini.</param>
        public static void updateApples(ref List<Apple> existingApples)
        {
            /* KOMENTAR
             * V dokumentaciji je funkcija brez argumenta: void updateApples(). Od nekod je treba dobiti jabolka.
            */
            for (int i = 0; i < existingApples.Count; i++)
            {
                if (existingApples[i].IsGold && existingApples[i].Counter > 0)
                {
                    existingApples[i].Counter--;
                }
                else
                {
                    existingApples[i].IsGold = false;
                    existingApples[i].WasGold = true;
                }
            }
        }

        public void decreaseCounter()
        {
            if (Counter > 0) Counter--;
            else
            {
                IsGold = false;
                WasGold = true;
            }
        }
    }
}
